﻿var appCommon = {
    developerOpenIds: null,
    isLocal: function () {
        return window.location.hostname == "local.bus.com";
    },
    isDeveloper: function () {
        if (appCommon.developerOpenIds == null || appCommon.developerOpenIds.length == 0) {
            return false;
        }
        var openId = appCommon.getOpenId();
        if (openId == null || openId.length == 0) {
            return false;
        }
        return appCommon.developerOpenIds.indexOf(openId) > -1;
    },
    getOpenId: function () {
        if (appCommon.isLocal()) {
            return "oXoTIvqUm2I1Bpoo9qlIKFSyceM0";
        }
        var param = "";
        param = $.cookie("wxopenid");
        //param = "oXoTIvqUm2I1Bpoo9qlIKFSyceM0";//方便测试，发布时请注释
        if (!Boolean(param)) {
            return param;
        }
        return param;
    },
    noEmpty: function (vl) {
        if (vl == "" || vl == null || vl == undefined) {
            return false;
        } else {
            return true;
        }
    },
    request: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    },
    toggle: function (obj) {
        var $this = $(obj);
        var toggleObj = $("#" + $this.attr("data-id"));
        toggleObj.toggle();
        switch ($this.attr("data-type")) {
            case "1": //改变图标方式切换
                var iconObj = $("#" + $this.attr("data-img"));
                if (toggleObj.css('display') == "block") {//
                    $this.html('<span class="icon" aria-hidden="true" data-icon="&#xe19d;">' + $this.text() + '</span>');
                } else {
                    $this.html('<span class="icon" aria-hidden="true" data-icon="&#xe19e;">' + $this.text() + '</span>');
                }
                break;
        }
    },
    getHM: function (second) {
        var result = "";
        var mins = Math.round(second / 60);
        if (mins >= 60) {
            result = Math.round(mins / 60) + "小时";
            mins = mins - 60;
        }
        if (mins != 0) {
            result = result + mins + "分钟";
        }
        return result;
    },
    dateFormat: function (date, fmt) {
        if (typeof (date) == "string") {
            date = new Date(Date.parse(date.replace(/-/g, "/")));
        }
        var o = {
            "M+": date.getMonth() + 1,                 //月份 
            "d+": date.getDate(),                    //日 
            "h+": date.getHours(),                   //小时 
            "m+": date.getMinutes(),                 //分 
            "s+": date.getSeconds(),                 //秒 
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
            "S": date.getMilliseconds()             //毫秒 
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    },
    remind: function (content) {
        layer.open({
            content: content,
            skin: 'msg',
            time: 2 //2秒后自动关闭
        });
    },
    confirm: function (content, callBack) {
        //询问框
        layer.open({
            content: content
          , btn: ['确定', '取消']
          , yes: callBack
        });
    },
    fullWindowHeight: function () {
        $("#content").height($(window).height());
    },
    formSerialize: function (formSel, formdate) {
        var element = $(formSel);
        if (!!formdate) {
            for (var key in formdate) {
                var $id = element.find('#' + key);
                var value = $.trim(formdate[key]).replace(/&nbsp;/g, '');
                var type = $id.attr('type');
                if ($id.hasClass("select2-hidden-accessible")) {
                    type = "select";
                }
                switch (type) {
                    case "checkbox":
                        if (value == "true") {
                            $id.attr("checked", 'checked');
                        } else {
                            $id.removeAttr("checked");
                        }
                        break;
                    case "select":
                        $id.val(value).trigger("change");
                        break;
                    default:
                        $id.val(value);
                        break;
                }
            };
            return false;
        }
        var postdata = {};
        element.find('input,select,textarea').each(function (r) {
            var $this = $(this);
            var id = $this.attr('id');
            var type = $this.attr('type');
            switch (type) {
                case "checkbox":
                    postdata[id] = $this.is(":checked");
                    break;
                default:
                    postdata[id] = $this.val();
                    break;
            }
        });
        return postdata;
    }

}